﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace ClipPlayer
{
    public class WinUtils
    {
        #region Native Methods
        //http://blogs.microsoft.co.il/alex_golesh/2009/09/20/wpf-quick-tip-how-to-get-wpf-window-client-area-size/
        [DllImport("user32.dll")]
        static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);

        [Serializable, StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;

            public RECT(int left_, int top_, int right_, int bottom_)
            {
                Left = left_;
                Top = top_;
                Right = right_;
                Bottom = bottom_;
            }

            public int Height { get { return Bottom - Top; } }
            public int Width { get { return Right - Left; } }
            public System.Drawing.Size Size { get { return new System.Drawing.Size(Width, Height); } }

            public System.Drawing.Point Location { get { return new System.Drawing.Point(Left, Top); } }

            // Handy method for converting to a System.Drawing.Rectangle
            public System.Windows.Rect ToRectangle()
            { return new System.Windows.Rect(Left, Top, Right, Bottom); }

            public static RECT FromRectangle(System.Windows.Rect rectangle)
            {
                return new System.Windows.Rect(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Bottom);
            }

            public override int GetHashCode()
            {
                return Left ^ ((Top << 13) | (Top >> 0x13))
                  ^ ((Width << 0x1a) | (Width >> 6))
                  ^ ((Height << 7) | (Height >> 0x19));
            }

            #region Operator overloads

            public static implicit operator System.Windows.Rect(RECT rect)
            {
                return rect.ToRectangle();
            }

            public static implicit operator RECT(System.Windows.Rect rect)
            {
                return FromRectangle(rect);
            }

            #endregion
        }

        public static RECT GetClientRect(IntPtr hWnd)
        {
            RECT result = new RECT();
            GetClientRect(hWnd, out result);
            return result;
        }
        #endregion
        public static int GetClientWidth(IntPtr hWnd)
        {
            RECT result = new RECT();
            GetClientRect(hWnd, out result);
            return result.Size.Width;
        }
    }//class
}//namespace
